package com.github.gdxmap.core;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.github.gdxmap.geo.LatLng;
import com.github.gdxmap.layer.Layer;

import java.util.ArrayList;
import java.util.List;

import javax.microedition.khronos.opengles.GL10;

/**
 * Created by free on 2016/12/6 0006.
 */
public class GdxMap implements ApplicationListener {

    protected OrthographicCamera camera;

    protected PolygonSpriteBatch polygonSpriteBatch;

    protected static MapOptions mapOptions;

    //所有的地图图层
    protected List<Layer> mapLayers;

    //绘制的地图图层
    protected List<Layer> drawLayers;

    private GdxMapListener gdxMapListener;

    protected float moveX=0;
    protected float moveY=0;
    protected float scale=1;

    @Override
    public void create() {
        GL20 gl = Gdx.graphics.getGL20();

        polygonSpriteBatch=new PolygonSpriteBatch();

        mapLayers=new ArrayList<>();
        drawLayers=new ArrayList<>();

        //清除屏幕
        gl.glClearColor(1f, 1.0f, 1.0f, 1);
        gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


        //取消持续渲染
        Gdx.graphics.setContinuousRendering(false);

        if (gdxMapListener!=null){
            gdxMapListener.loadFinish();
        }
    }

    @Override
    public void resize (int width, int height) {
        camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        camera.position.x = Gdx.graphics.getWidth()/2;
        camera.position.y = Gdx.graphics.getHeight()/2;

    }

    @Override
    public void render() {
        camera.update();
        camera.translate(moveX,moveY,0);

        polygonSpriteBatch.setProjectionMatrix(camera.combined);

        for(int i=0;i<drawLayers.size();i++){
            Layer layer=drawLayers.get(i);

            layer.move(moveX,moveY);
            layer.scale(scale);

            layer.render();
        }
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {
        polygonSpriteBatch.dispose();
    }
    public void setZoom(int maxZoom,int minZoom){

    }
    public void zoomIn(){

    }
    public void zoomOut(){

    }
    public void moveTo(LatLng center){
        moveTo(center,true);
    }
    public void moveTo(LatLng center,boolean isAnimation){

    }

    /**
     * 地图相对移动x像素点，y像素点
     * @param fx
     * @param fy
     */
    public void moveBy(float fx,float fy){
        this.moveX=fx;
        this.moveY=fy;

        Gdx.graphics.requestRendering();
    }
    public void rotate(short angle){

    }
    public void stop(){

    }
    public LatLng getCenter(){
        return new LatLng();
    }
    public double distance(LatLng latlng1,LatLng latlng2){
        double distance=0;

        return distance;
    }

    public void setGdxMapListener(GdxMapListener gdxMapListener) {
        this.gdxMapListener = gdxMapListener;
    }

    public static MapOptions getMapOptions() {
        return mapOptions;
    }

    public void setMapOptions(MapOptions mapOptions) {
        this.mapOptions = mapOptions;

        if (mapOptions.getLayers()!=null){
            setDrawer(mapOptions.getLayers());

            mapLayers.addAll(mapOptions.getLayers());

            for(Layer layer:mapOptions.getLayers()){
                layer.addRender();

                drawLayers.add(layer);
            }
        }
    }

    /**
     * 为每个图层设置矢量渲染器
     */
    private void setDrawer(List<Layer> layerList){
        for(Layer layer:layerList){
            layer.setPolygonSpriteBatch(polygonSpriteBatch);
        }
    }
}
